package org.jetnuke.modules.admin.blocks;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.Configuration;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Blocks;
import org.jetnuke.entities.HeadLine;
import org.jetnuke.util.JetUtil;

public class InsRss implements IAction {
String title;
String url;
String rss;
	/* (non-Javadoc)
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo, java.lang.String, org.jetnuke.common.IUserInfo, org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (ai == null || !ai.IsSuperUser())
			return null;//Error;
		//vc.put("ls",DataAccessDriver.getInst().newHeadLinesModel().selectAll());
		Blocks block=new Blocks();
		block.setTITLE(title);
		block.setACTIVE(1);
		block.setPATH("RssNews");
		block.setPOSITION("r");
		block.setWEIGHT(1);
		block.setTIME("0");
		if(rss.equals("")){
			block.setRSS_URL(url);
		}else{
			HeadLine hl=DataAccessDriver.getInst().newHeadLinesDAO().retrieveByPK(Integer.parseInt(rss));
			block.setRSS_URL(hl.getHEADLINESURL());
		}
		DataAccessDriver.getInst().newBlockDAO().addNew(block);
		Configuration cfg = new Configuration(request.getSession(true).getServletContext());
		request.getSession(true).getServletContext().setAttribute("_cfg", cfg);

		JetUtil.redirect(vc,response,request.getContextPath()+"/do/admin/show_blocks");
		return null;//ew ActionForward(op,info);
	}

public void setRss(String rss) {
	this.rss = rss.trim();
}
public void setTitle(String title) {
	this.title = title;
}
public void setUrl(String url) {
	this.url = url;
}
}
